/*
 *    Copyright 2010-2013 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.mybatis.jpetstore.web.actions;

import java.util.List;

import org.mybatis.jpetstore.domain.Category;
import org.mybatis.jpetstore.domain.Item;
import org.mybatis.jpetstore.domain.Product;
import org.mybatis.jpetstore.service.CatalogService;
import org.mybatis.jpetstore.web.bean.CatalogBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Eduardo Macarron
 */
@RequestMapping("/catalog")
@Controller
public class CatalogController extends AbstractController {

	private static final long serialVersionUID = 5849523372175050635L;

	private static final String MAIN = "/catalog/Main";
	private static final String VIEW_CATEGORY = "/catalog/Category";
	private static final String VIEW_PRODUCT = "/catalog/Product";
	private static final String VIEW_ITEM = "/catalog/Item";
	private static final String SEARCH_PRODUCTS = "/catalog/SearchProducts";

	@Autowired
	private CatalogService catalogService;

	@RequestMapping({"", "list"})
	public String viewMain() {
		return (MAIN);
	}

	@RequestMapping("viewCategory")
	public String viewCategory(@ModelAttribute CatalogBean catalogBean) {
		String categoryId = catalogBean.getCategoryId();
		if (catalogBean.getCategoryId() != null) {
			List<Product> productList = catalogService.getProductListByCategory(categoryId);
			Category category = catalogService.getCategory(categoryId);
			catalogBean.setProductList(productList);
			catalogBean.setCategory(category);
		}
		return (VIEW_CATEGORY);
	}

	@RequestMapping("viewProduct")
	public String viewProduct(@ModelAttribute CatalogBean catalogBean) {
		String productId = catalogBean.getProductId();
		if (productId != null) {
			List<Item> itemList = catalogService.getItemListByProduct(productId);
			Product product = catalogService.getProduct(productId);
			catalogBean.setItemList(itemList);
			catalogBean.setProduct(product);
		}
		return (VIEW_PRODUCT);
	}

	@RequestMapping("viewItem")
	public String viewItem(@ModelAttribute CatalogBean catalogBean) {
		Item item = catalogService.getItem(catalogBean.getItemId());
		Product product = item.getProduct();
		catalogBean.setItem(item);
		catalogBean.setProduct(product);
		return (VIEW_ITEM);
	}

	@RequestMapping("searchProducts")
	public String searchProducts(@ModelAttribute CatalogBean catalogBean, ModelMap model) {
		String keyword = catalogBean.getKeyword();
		if (keyword == null || keyword.length() < 1) {
			model.addAttribute("message", "Please enter a keyword to search for, then press the search button.");
			return (ERROR);
		} else {
			List<Product> productList = catalogService.searchProductList(keyword.toLowerCase());
			catalogBean.setProductList(productList);
			return (SEARCH_PRODUCTS);
		}
	}
}
